<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\Auth;
use app\models\AuthItemChild;
use app\models\UserLogin;
use app\tools\Re;
use Yii;
use yii\helpers\ArrayHelper;
use app\models\Admin;
use app\models\ExhibitionApply;
header('Access-Control-Allow-Origin: *');

class PermissionController extends BaseController
{
    public function actionAuthList()
    {
        $page = Yii::$app->request->get('page', 0);
        $query = Auth::find()->where(['type' => 2]);
        $auth = $query->limit(Auth::PAGE_SIZE)->offset($page*Auth::PAGE_SIZE)->orderBy("sort asc")->all();
        $fields = ['name', 'description', 'sort', 'created_at'];
        return Re::data([
            'data' => Base::buildParamsList($auth, $fields),
            'totalPage' => $query->count(),
            'currentPage' => $page,
            'perPage' => Auth::PAGE_SIZE
        ]);
    }

    public function actionCreateAuth()
    {
        $post = Yii::$app->request->post();
        if (empty($post['name']) || empty($post['description'])) {
            return Re::data(null, '缺少参数', Re::ERROR);
        }
        $auth = Auth::find()->where(['name' =>$post['name'], 'type' => 2])->one();
        if ($auth) {
            return Re::data(null, '权限已存在', Re::ERROR);
        }
        $auth = new Auth();
        $auth->name = $post['name'];
        $auth->description = $post['description'];
        $auth->type = 2;
        $auth->sort = $post['sort'];
        $auth->created_at = time();
        $auth->updated_at = time();
        $auth->save();
        $authItemChild = new AuthItemChild();
        $authItemChild->parent = "超级管理员";
        $authItemChild->child = $post['name'];
        $authItemChild->save();
        return Re::data();
    }

    public function actionDeleteAuth()
    {
        $name = Yii::$app->request->post('name');
        $auth = Auth::find()->where(['name' =>$name, 'type' => 2])->one();
        if ($auth) {
            $auth->delete();
            AuthItemChild::deleteAll(['child' => $name]);
            return Re::data();
        }
        return Re::data(null, '权限不存在', Re::ERROR);
    }

    public function actionSelectData(){
        $list = Auth::find()->where(['type'=>2])->orderBy("sort asc")->all();
        return Re::data(['data' => $list]);
    }

    public function actionGetRoleAuth(){
        $post = Yii::$app->request->post();
        if (empty($post['name'])) {
            return Re::data(null, '缺少参数', Re::ERROR);
        }
        $list = Auth::find()
        ->leftJoin("auth_item_child", "auth_item.name = auth_item_child.child")
        ->where(['parent'=>$post['name']])
        ->select("name,description,sort")
        ->orderBy("sort asc")
        ->all();
        return Re::data(['data' => $list]);
    }

    public function actionGetUserAuth(){
        $user_token = Yii::$app->request->get("user_token");
        $user_id = 0;
        if (empty($user_token)) {
            return Re::data(null, '缺少参数', Re::ERROR);
        }
        if (preg_match('/^Bearer\s+(.*?)$/', $user_token, $matches)) {
            $user_token = $matches[1];
        }
        $user_login = UserLogin::find()->where(['token' => $user_token])->orderBy("created_at desc")->one();
        if ($user_login) {
            $user_id = $user_login->user_id;
            $query = AuthItemChild::find()
            ->leftJoin("auth_assignment", "auth_assignment.item_name = auth_item_child.parent")
            ->where(['auth_assignment.user_id'=>$user_id]);
            $list = $query->select("child")->column();
            return Re::data(['data' => $list]);
        }
        return Re::data(['data' => []]);
    }
}
